package com.yanqu.road.server.logic.task.condition.weekend;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.kuang.KuangConfig;
import com.yanqu.road.entity.kuang.config.entity.KuangTimeConfig;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.entity.task.TaskInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.logic.task.BaseCondition;
import com.yanqu.road.server.logic.task.BaseTaskData;
import com.yanqu.road.server.logic.task.TaskConditionMail;
import com.yanqu.road.server.manger.*;
import com.yanqu.road.server.manger.config.TaskMgr;
import com.yanqu.road.server.manger.kuang.KuangMgr;
import com.yanqu.road.server.manger.systemgroup.CrossSystemGroupMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.List;

public class WeekendDayRechargeCondition extends BaseCondition {


    public WeekendDayRechargeCondition(TaskConditionInfo taskConditionInfo, BaseTaskData baseTaskData, BigInteger value, Object initArgs) {
        super(taskConditionInfo, baseTaskData, value, initArgs);
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.WeekendDayRecharge.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.WeekendDayRecharge.getValue(), handle);
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        List<BigInteger> paramList = taskConditionInfo.getParamList();
        return getValue().compareTo(BigInteger.valueOf(paramList.get(3).intValue())) >= 0;
    }

    @Override
    protected void changeValue(Object object) {
        TaskInfo taskInfo = TaskMgr.getTaskInfoById(taskConditionInfo.getTaskId());
        if (taskInfo == null) {
            return;
        }

        int systemId = taskInfo.getLittleType();
        long serverId = GameServer.getInstance().getServerId();
        int channelId = ServerListMgr.getChannelId(serverId);
        List<Integer> nowWeekSystemIds = SystemLoopMgr.getNowWeekSystemIds(channelId, serverId);
        if (!nowWeekSystemIds.contains(systemId)) {
            return;
        }

        if (!isInActiveDay(systemId)) {
            return;
        }

        long value = (long) object;
        if (value > 0 && value > getValue().longValue()) {
            setValue(BigInteger.valueOf(value));
        }
    }

    private boolean isInActiveDay(int systemId) {
        long currentTime = DateHelper.getCurrentTime();
        if (systemId == eSystemId.UnionCampWar.getValue()) {
            long startZeroTime = DateHelper.getZeroTimeStamp(ConfigMgr.getUnionCampWarConfig().getNowWeekBattleTime() * DateHelper.SECOND_MILLIONS);
            long endZeroTime = DateHelper.getZeroTimeStamp(ConfigMgr.getUnionCampWarConfig().getNowWeekEndTime() * DateHelper.SECOND_MILLIONS) + DateHelper.DAY_MILLIONS;
            if (currentTime >= startZeroTime && currentTime <= endZeroTime) {
                return true;
            }
        } else if (systemId == eSystemId.Kuang.getValue()) {
            KuangConfig config = KuangMgr.getConfig();
            if (config == null) {
                return false;
            }
            KuangTimeConfig timeConfig = config.getTimeConfig();
            if (timeConfig == null) {
                return false;
            }
            long startZeroTime = DateHelper.getZeroTimeStamp(timeConfig.getDay1BeginTime());
            long endZeroTime = DateHelper.getZeroTimeStamp(timeConfig.getDay2BeginTime()) + DateHelper.DAY_MILLIONS;
            if (currentTime >= startZeroTime && currentTime <= endZeroTime) {
                return true;
            }
        } else if (systemId == eSystemId.NewMonopolyMarket.getValue()){
            int weekId = GameConfig.getSystemThisWeekId();
            if(CrossSystemGroupMgr.getSystemServerGroup(weekId, systemId) == null){
                return false;
            }
            int now = DateHelper.getCurrentSecond();
            int endTime = NewMonopolyMarketCommonMgr.getResetOfficeTime(now, ConfigMgr.getOfficeEndSecondNew());
            //周日0点
            int zeroTime = DateHelper.getZeroTime(endTime);
            int startZeroTime = zeroTime - (int)DateHelper.DAY_SECONDS;
            int endZeroTime = zeroTime + (int)DateHelper.DAY_SECONDS;
            if(now >= startZeroTime && now < endZeroTime){
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean isNeedAutoSendMail() {
        TaskInfo taskInfo = TaskMgr.getTaskInfoById(taskConditionInfo.getTaskId());
        if (taskInfo == null) {
            return false;
        }
        if (taskInfo.getLittleType() != eSystemId.UnionCampWar.getValue()) {
            return isCompleted(player);
        }
        return false;
    }

    @Override
    public String getMailKey() {
        TaskInfo taskInfo = TaskMgr.getTaskInfoById(taskConditionInfo.getTaskId());
        return taskInfo.getType() + "_" + taskInfo.getLittleType() + "_" + taskConditionInfo.getType();
    }

    public TaskConditionMail newConditionMail(){
        TaskInfo taskInfo = TaskMgr.getTaskInfoById(taskConditionInfo.getTaskId());
        String language = player.getLanguage();
        Integer systemId = taskInfo.getLittleType();
        String systemNameKey = "System_" + systemId;
        String systemName = MultipleLanguageMgr.getContent(systemNameKey, language);

        String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.DAY_RECHARGE_REWARD_TITLE, language), systemName);
        String content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.DAY_RECHARGE_REWARD_DESC, language), systemName);

        TaskConditionMail taskConditionMail = new TaskConditionMail(getMailKey(), mailTitle, content);
        return taskConditionMail;
    }

}

